package cn.blue.utils;

import cn.blue.exception.GlobalException;
import cn.blue.exception.GlobalExceptionCodeEnum;
import lombok.Data;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Data
public class AssertUtil {

    public static final String PHONE = "^((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\\d{8}$";

    //手机的正则表达式
    private static final Pattern CHINA_PATTERN_PHONE = Pattern.compile("^((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\\d{8}$");


    /**--------------------------------------------------------
    手机号断言
    --------------------------------------------------------**/
    public static void isPhone(String phone,String message){
        Matcher m = CHINA_PATTERN_PHONE.matcher(phone);
        if(!m.matches()){
            throw new RuntimeException(message);
        }
    }


    /**--------------------------------------------------------
    断言 不为空，如果为空，抛异常
    --------------------------------------------------------**/
    public static void isNotEmpty(String text, GlobalExceptionCodeEnum globalExceptionCodeEnum) {
        if (text == null || text.trim().length() == 0) {
            throw new GlobalException(globalExceptionCodeEnum);
        }
    }

//    //断言不为空
//    public static void isEmpty(String text, GlobalExceptionCodeEnum globalExceptionCodeEnum) {
//        if (text != null || text.trim().length() != 0) {
//            throw new GlobalException(globalExceptionCodeEnum);
//        }
//    }


    /**--------------------------------------------------------
    断言对象为空
    --------------------------------------------------------**/
    public static void isNull(Object obj , GlobalExceptionCodeEnum globalExceptionCodeEnum){
        if(obj != null){
            throw new GlobalException(globalExceptionCodeEnum);
        }
    }
    public static void isNotNull(Object obj , GlobalExceptionCodeEnum globalExceptionCodeEnum){
        if(obj == null){
            throw new GlobalException(globalExceptionCodeEnum);
        }
    }

    /**--------------------------------------------------------
     断言false,如果为true,我报错
     --------------------------------------------------------**/
    public static void isFalse(boolean isFalse , String message){
        if(isFalse){
            throw new RuntimeException(message);
        }
    }
    public static void isTrue(boolean isTrue , String message){
        if(!isTrue){
            throw new RuntimeException(message);
        }
    }


    /**--------------------------------------------------------
     断言两个字符串一致
     --------------------------------------------------------**/
    public static void isEquals(String s1,String s2 , GlobalExceptionCodeEnum globalExceptionCodeEnum){
        isNotEmpty(s1,GlobalExceptionCodeEnum.PARAM_IS_NULL);
        isNotEmpty(s2, GlobalExceptionCodeEnum.PARAM_IS_NULL);
        if(!s1.equals(s2)){
            throw new GlobalException(globalExceptionCodeEnum);
        }
    }
    public static void isEqualsTrim(String s1,String s2 , GlobalExceptionCodeEnum globalExceptionCodeEnum){
        isNotEmpty(s1, GlobalExceptionCodeEnum.PARAM_IS_NULL);
        isNotEmpty(s2, GlobalExceptionCodeEnum.PARAM_IS_NULL);
        if(!s1.trim().equals(s2.trim())){
            throw new GlobalException(globalExceptionCodeEnum);
        }
    }

    public static void isEqualsIgnoreCase(String s1,String s2 , GlobalExceptionCodeEnum globalExceptionCodeEnum){
        isNotEmpty(s1, GlobalExceptionCodeEnum.PARAM_IS_NULL);
        isNotEmpty(s2, GlobalExceptionCodeEnum.PARAM_IS_NULL);
        if(!s1.trim().equalsIgnoreCase(s2.trim())){
            throw new GlobalException(globalExceptionCodeEnum);
        }
    }

}
